package cn.learn.druid;

import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.DruidDataSourceStatLoggerAdapter;
import com.alibaba.druid.pool.DruidDataSourceStatValue;
import com.alibaba.druid.stat.JdbcSqlStatValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 保存DruidDataSource的监控记录，这样可以监控到sql的质量、使用频率，以便sql的优化
 *
 * @author huangyezhan
 * @date 2020年02月16日14:22
 */
public class MyStatLogger extends DruidDataSourceStatLoggerAdapter implements DruidDataSourceStatLogger {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void log(DruidDataSourceStatValue statValue) {

        //数据库连接
        String url = statValue.getUrl();
        //数据库账号
        String userName = statValue.getUserName();
        //要好好看一下sql的获取
        List<JdbcSqlStatValue> sqlList = statValue.getSqlList();
        if (statValue.getSqlList().size() > 0) {
            for (JdbcSqlStatValue sqlStat : sqlList) {
                String sql = sqlStat.getSql();
                logger.info("sql：{}",sql);
            }
        }
        logger.info("***************************************************");
        logger.info("                  监控数据持久化                     ");
        logger.info("***************************************************");


    }


}
